package com.yunhai.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.tms.Store;
import com.yunhai.model.vo.tms.StoreAdd;
import com.yunhai.model.vo.tms.StoreQuery;
import com.yunhai.model.vo.tms.StoreUpdate;
import com.yunhai.store.mapper.StoreMapper;
import com.yunhai.store.service.StoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private StoreMapper storeMapper;

    /**
     * 获取 Store 分页列表
     */
    @Transactional
    public IPage getStorePagination(PaginationVo paginationVo, StoreQuery storeQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<Store> lqs = new LambdaQueryWrapper();
        storeMapper.selectPage(page, lqs);

        List<Store> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return null;
    }


    /**
     * 获取 Store 所有数据
     *
     */
    @Transactional
    public List<Store> getStoreAllList( StoreQuery storeQuery ) throws Exception {

        LambdaQueryWrapper<Store> lqs = new LambdaQueryWrapper();
        List<Store> list = storeMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 Store 详情
     */
    @Transactional
    public Store getStoreDetail( Long id) throws Exception {

        Store store = storeMapper.selectById(id);
        return store;
    }


    /**
     * 新增 Store
     * */
    @Transactional
    public void addStore(StoreAdd storeAdd) throws Exception {

        Store store = new Store();
        ReflectionCopy.copyProperties( store, storeAdd );
        storeMapper.insert(store);
        return;
    }


    /**
     * 更新 Store
     * */
    @Transactional
    public void updateStore( StoreUpdate storeUpdate) throws Exception {

        if( storeUpdate.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        Store store = new Store();
        ReflectionCopy.copyProperties( store, storeUpdate );
        storeMapper.updateById(store);
        return;
    }

    /**
     * 删除 Store
     * */
    @Transactional
    public void batchRemoveStore( List<Long> ids ) throws Exception {


        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        storeMapper.deleteBatchIds(ids);
    }

}

